package com.junzhiit.nengji.api.web.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bainaeco.framework.base.spring.Context;
import com.junzhiit.nengji.api.service.OrdersService;

@Controller
public class WeChatMessageController extends Context{
	
	
	@Autowired
	private OrdersService ordersService;
	
	
	@RequestMapping(value = "/wechat/notify",method = RequestMethod.POST)
	public void notify(HttpServletRequest request,
			HttpServletResponse response){
		try {
			InputStream inputStream = request.getInputStream();
			String xml = IOUtils.toString(inputStream, "UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(ordersService.getWechatNotify(xml));  
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wechat/refund/notify",method = RequestMethod.POST)
	public void refundNotify(HttpServletRequest request,
			HttpServletResponse response){
		try {
			InputStream inputStream = request.getInputStream();
			String xml = IOUtils.toString(inputStream, "UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(ordersService.refundNotify(xml));  
	        out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
